package com.derive.education_service.web.admin.controller;

import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.web.admin.bean.dto.AdminAddDto;
import com.derive.education_service.web.admin.bean.dto.AdminUpDto;
import com.derive.education_service.web.admin.bean.dto.QueryParam;
import com.derive.education_service.web.admin.service.AdminMegService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@RestController
@Slf4j
@Validated
@CrossOrigin
@RequestMapping("adminMeg")
public class AdminMegController {

    @Autowired
    private AdminMegService adminMegService;

    /**
     * 添加管理员 -- 超管权限
     * @param request
     * @param adminAddDto
     * @return
     */
    @PostMapping("addAdminInfo")
    public ResponseMessage addAdminInfo(HttpServletRequest request,@Validated @RequestBody AdminAddDto adminAddDto) {
        return adminMegService.addAdminInfo(request, adminAddDto);
    }

    /**
     * 管理员编辑自己信息
     * @param request
     * @param adminUpDto
     * @return
     */
    @PostMapping("upAdmin")
    public ResponseMessage upAdmin(HttpServletRequest request,@Validated @RequestBody AdminUpDto adminUpDto) {
        return adminMegService.upAdmin(request, adminUpDto);
    }

    /**
     * 给管理员分配角色权限 -- 超管权限(为了安全，可以加密)
     * @param request
     * @param roleId
     * @param adminId
     * @return
     */
    @PostMapping("allotRole")
    public ResponseMessage allotRole(HttpServletRequest request, @NotNull(message = "角色id不能为空") int roleId, @NotNull(message = "分配的管理员id不能为空") int adminId) {
        return adminMegService.allotRole(request, roleId, adminId);
    }

    /**
     * 禁用管理员 -- 超管权限 isState == false
     * @param request
     * @param adminId
     * @return
     */
    @GetMapping("banAdmin")
    public ResponseMessage banAdmin(HttpServletRequest request,@NotNull(message = "禁用管理员id不能为空") int adminId) {
        return adminMegService.banAdmin(request, adminId);
    }

    /**
     * 启用管理员 -- 超管权限 isState == true
     * @param request
     * @param adminId
     * @return
     */
    @GetMapping("startAdmin")
    public ResponseMessage startAdmin(HttpServletRequest request,@NotNull(message = "启用管理员id不能为空") int adminId) {
        return adminMegService.startAdmin(request, adminId);
    }

    /**
     * 分页查询管理员列表
     * @param request
     * @param queryParam
     * @return
     */
    @PostMapping("getAdminList")
    public ResponseMessage getAdminList(HttpServletRequest request,@Validated @RequestBody QueryParam queryParam) {
        return adminMegService.getAdminList(request, queryParam);
    }
}
